﻿using System.Collections.Generic;

namespace IceCS.DesignPattern.Core.ServiceLocatorPattern.Member
{
    public class MemberCache
    {
        private readonly List<IMemberService> _services;

        public MemberCache()
        {
            _services = new List<IMemberService>();
        }

        public IMemberService this[MemberServiceType serviceName] => GetService(serviceName);

        public IMemberService GetService(MemberServiceType serviceName)
        {
            foreach (IMemberService service in _services)
            {
                if (service.GetName() == serviceName)
                {
                    return service;
                }
            }
            return null;
        }

        public void AddService(IMemberService newService)
        {
            bool exists = false;
            foreach (IMemberService service in _services)
            {
                if (service.GetName() == newService.GetName())
                {
                    exists = true;
                }
            }
            if (!exists)
            {
                _services.Add(newService);
            }
        }

        public void Clear()
        {
            _services.Clear();
        }
    }
}
